<template>
  <!-- 质检问卷 -->
  <div class="questionnaire">
    <ContentWrap>

      <FormContainer :column="4">
        <el-form ref="form" :model="formData" :rules="formRules" label-width="100px">
          <el-form-item prop="title" label="问卷名称">
            <el-input v-model.trim="formData.title" placeholder="请输入问卷名称" clearable />
          </el-form-item>
          <el-form-item prop="state" label="问卷状态">
            <el-select v-model="formData.state">
              <el-option label="全部" value />
              <el-option label="未发布" :value="0" />
              <el-option label="已发布" :value="1" />
            </el-select>
          </el-form-item>
          <el-form-item prop="createTime" :span="7" label="创建日期">
            <!-- <el-date-picker v-model="formData.createTime" type="date" placeholder="选择日期" /> -->
            <el-date-picker
              v-model="formData.createTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
          <el-form-item :span="5">
            <Button @click="resetForm">重置</Button>
            <Button type="primary" enter @click="queryHandle">查询</Button>
          </el-form-item>
        </el-form>
      </FormContainer>
      <div class="button-container" style="padding-top:0">
        <Button type="primary" @click="add">新增</Button>
        <Button type="primary" @click="edit()">编辑</Button>
        <Button type="danger" @click="delet()">删除</Button>
        <Button type="primary" @click="release()">发布</Button>
      </div>
      <table-box ref="table" :other-filters="otherFilters" no-request :table-config="tableConfig" @row-click="(val)=>{tempData=val.row}">
        <el-table-column slot="before" label="序号" type="index" align="center" width="55"></el-table-column>
      </table-box>
    </ContentWrap>
    <!-- 查看预览 -->
    <Preview ref="questionnairePreview"></Preview>
  </div>
</template>
<script>
import ContentWrap from '@/components/ContentWrap'
import FormContainer from '@/components/FormContainer'
import Button from '@/components/Button'
import Preview from './components/Preview'
import { filtersRaw, addOneDay } from '@/utils'
import {
  questionnaireDelet,
  questionnaireUpdate
} from '@/api/questionnaire'
export default {
  name: 'QuestionnaireQuality',
  components: {
    ContentWrap,
    FormContainer,
    Button,
    Preview
  },
  data() {
    return {
      loading: false,
      formData: {
        title: '',
        questionnaireSort: 1,
        createTime: [],
        state: null
      },
      tempData: null,
      formRules: {},
      otherFilters: [{ 'id': '', 'property': 'questionnaireSort', 'operator': '=', 'value': 1 }],
      tableConfig: {
        interfacePath: '/call-center/api/v1/questionnaire/page',
        sort: 'createTime',
        order: 'desc',
        pageFlag: true,
        hideFuncBar: true,
        // 表格字段动态配置
        setting: [
          {
            prop: 'title',
            label: '问卷名称',
            className: 'table-click-text',
            isButton: true,
            clickFun: row => {
              this.detail(row)
            }
          },
          {
            prop: 'questionnaireSort',
            label: '问卷分类',
            formatData: val => {
              return val.questionnaireSort === 1 ? '质检问卷' : '回访问卷'
            },
            minWidth: 100
          },
          {
            prop: 'questionAmount',
            label: '题目数量',
            minWidth: 100
          },
          {
            prop: 'remark',
            label: '备注'
          },
          {
            prop: 'state',
            label: '问卷状态',
            formatData: val => {
              return val.state === 1 ? '已发布' : '未发布'
            },
            minWidth: 100
          },
          {
            prop: 'createBy',
            label: '创建人'
          },
          {
            prop: 'createTime',
            label: '创建时间',
            formatData: val => {
              return val.createTime ? val.createTime.substring(0, 19) : ''
            },
            minWidth: 100
          },
          {
            prop: 'lastUpdateBy',
            label: '最后修改人',
            minWidth: 120
          },
          {
            prop: 'lastUpdateTime',
            label: '最后修改时间',
            formatData: val => {
              return val.lastUpdateTime ? val.lastUpdateTime.substring(0, 19) : ''
            },
            minWidth: 120
          }
        ]
      },
      pickerOptions: {
        disabledDate(date) {
          return date > new Date()
        }
      }
    }
  },
  activated() {
    this.refreshData('opCondition')
  },
  methods: {
    // 根据条件查询
    queryHandle() {
      const list = []
      for (const i in this.formData) {
        if (i === 'createTime') {
          if (this.formData[i] && this.formData[i].length) {
            list.push(filtersRaw(i, this.formData[i][0], '>='))
            list.push(filtersRaw(i, addOneDay(this.formData[i][1]), '<='))
          }
        } else {
          if (this.formData[i] || this.formData[i] === 0) {
            if (i === 'state' || i === 'questionnaireSort') {
              list.push(filtersRaw(i, this.formData[i], '='))
            } else {
              list.push(filtersRaw(i, this.formData[i], 'like'))
            }
          }
        }
      }
      this.otherFilters = list
      this.$nextTick(() => {
        this.refreshData('opCondition')
      })
    },
    // 新增
    add() {
      window.sessionStorage.removeItem('questionnaireId')
      this.$router.push({ name: 'QuestionnaireAddEdit', query: { type: 1, title: '质检问卷新增' }})
    },
    // 编辑
    edit() {
      if (!this.tempData) {
        return this.$message.info('请先选择要编辑的数据')
      }
      if (this.tempData.state === 1) {
        return this.$message.info('不能修改已发布问卷')
      }
      window.sessionStorage.questionnaireId = this.tempData.id
      this.$router.push({ name: 'QuestionnaireAddEdit' })
    },
    // 删除
    delet() {
      if (!this.tempData) {
        return this.$message.info('请先选择要删除的数据')
      }
      if (this.tempData.state === 1) {
        return this.$message.info('不能删除已发布问卷')
      }
      var that = this
      this.$confirm('是否确定删除此问卷?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          questionnaireDelet(this.tempData.id)
            .then(res => {
              if (res.success === 1) {
                that.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                that.refreshData('onPage')
              }
            })
            .catch(er => {})
        })
        .catch(() => {})
    },
    // 详情
    detail(row) {
      this.$router.push({
        name: 'QuestionnaireTestPreview',
        query: { id: row.id }
      })
    },
    // 发布
    release() {
      if (!this.tempData) {
        return this.$message.info('请先选择要发布的数据')
      }
      if (this.tempData.state === 1) {
        return this.$message.info('该问卷已经发布,无需再次发布')
      }
      const obj = JSON.parse(JSON.stringify(this.tempData))
      obj.state = 1
      this.$confirm('是否确定发布此问卷?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          questionnaireUpdate(obj).then(res => {
            if (res.success === 1) {
              this.$message({
                type: 'success',
                message: '发布成功!'
              })
              this.refreshData('onPage')
            }
          })
        })
        .catch(() => {})
    },
    // 清空查询项
    resetForm() {
      this.$refs.form.resetFields()
    },
    // 刷新数据
    refreshData(type) {
      this.$refs.table.initTableData(type)
    },

    testPaper(row) {
      this.$router.push({
        path: '/questionnaireTestPaper',
        query: { id: row.id }
      })
    }
  }
}
</script>
<style lang='scss' module>
</style>
